package com.ruoyi.erp.controller;

import java.util.List;

import com.ruoyi.erp.domain.BankAccount;
import com.ruoyi.erp.domain.PayablePlan;
import com.ruoyi.erp.service.IBankAccountService;
import com.ruoyi.erp.service.IPayablePlanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.PayableAccount;
import com.ruoyi.erp.service.IPayableAccountService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 付款记录Controller
 * 
 * @author sauger
 * @date 2022-04-04
 */
@Controller
@RequestMapping("/erp/payable/payable")
public class PayableAccountController extends BaseController
{
    private String prefix = "erp/payable/payable";

    @Autowired
    private IPayableAccountService payableAccountService;
    @Autowired
    private IPayablePlanService payablePlanService;
    @Autowired
    private IBankAccountService bankAccountService;

    @RequiresPermissions("erp:payable:payable:view")
    @GetMapping()
    public String account(ModelMap mmap)
    {
        List<PayablePlan> plans = payablePlanService.selectPayablePlanList(null);
        BankAccount search = new BankAccount();
        search.setIsFlow(1);
        List<BankAccount> banks = bankAccountService.selectBankAccountList(search);
        mmap.put("plans", plans);
        mmap.put("banks" , banks);
        return prefix + "/index";
    }

    @RequiresPermissions("erp:payable:payable:view")
    @GetMapping("/{id}")
    public String account(@PathVariable("id") Integer id, ModelMap mmap)
    {
        PayablePlan plan = payablePlanService.selectPayablePlanById(id);
        List<PayablePlan> plans = payablePlanService.selectPayablePlanList(null);
        BankAccount search = new BankAccount();
        search.setIsFlow(1);
        List<BankAccount> banks = bankAccountService.selectBankAccountList(search);
        mmap.put("plans", plans);
        mmap.put("banks" , banks);
        mmap.put("plan", plan);
        return prefix + "/plan_payable";
    }



    /**
     * 查询付款记录列表
     */
    @RequiresPermissions("erp:payable:payable:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PayableAccount payableAccount)
    {
        startPage();
        List<PayableAccount> list = payableAccountService.selectPayableAccountList(payableAccount);
        return getDataTable(list);
    }

    /**
     * 导出付款记录列表
     */
    @RequiresPermissions("erp:payable:payable:export")
    @Log(title = "付款记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PayableAccount payableAccount)
    {
        List<PayableAccount> list = payableAccountService.selectPayableAccountList(payableAccount);
        ExcelUtil<PayableAccount> util = new ExcelUtil<PayableAccount>(PayableAccount.class);
        return util.exportExcel(list, "付款记录数据");
    }

    /**
     * 新增付款记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/add/{id}")
    public String addByPlanId(@PathVariable("id") Integer id, ModelMap mmap)
    {
        PayablePlan plan = payablePlanService.selectPayablePlanById(id);
        mmap.put("plan", plan);
        return prefix + "/add";
    }

    /**
     * 新增保存付款记录
     */
    @RequiresPermissions("erp:payable:payable:add")
    @Log(title = "付款记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PayableAccount payableAccount)
    {
        return toAjax(payableAccountService.insertPayableAccount(payableAccount));
    }

    /**
     * 修改付款记录
     */
    @RequiresPermissions("erp:payable:payable:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        PayableAccount payableAccount = payableAccountService.selectPayableAccountById(id);
        mmap.put("payableAccount", payableAccount);
        return prefix + "/edit";
    }

    /**
     * 修改保存付款记录
     */
    @RequiresPermissions("erp:payable:payable:edit")
    @Log(title = "付款记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PayableAccount payableAccount)
    {
        return toAjax(payableAccountService.updatePayableAccount(payableAccount));
    }

    /**
     * 删除付款记录
     */
    @RequiresPermissions("erp:payable:payable:remove")
    @Log(title = "付款记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(payableAccountService.deletePayableAccountByIds(ids));
    }
}
